package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Collect;
import com.net.tkc.pro.entity.FraudType;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.impl.FraudTypeImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Type/")
public class FraudTypeController {
    ResultEntity resultEntity = null;
    @Resource
    FraudTypeImpl fraudTypeimpl;

    /**
     * 添加记录
     * @param Userid 用户id
     * @param Test 内容
     * @param method 被骗平台
     * @param amount 金额
     * @param FraudType 诈骗类型
     * @return
     */

    @PostMapping("Type")
    private String addCollect(
            @RequestParam("Userid") int Userid,
            @RequestParam("Test") String Test,
            @RequestParam("Method") String method,
            @RequestParam("amount") String amount,
            @RequestParam("FraudType") String FraudType
    ){
        resultEntity = new ResultEntity();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=new Date();
        String Time=dateFormat.format(date);
        FraudType fraudType1=new FraudType();
        fraudType1.setUserid(Userid);
        fraudType1.setNowTime(Time);
        fraudType1.setTest(Test);
        fraudType1.setMethod(method);
        fraudType1.setAmount(amount);
        fraudType1.setFraudType(FraudType);
        try{
            int result = fraudTypeimpl.addType(fraudType1);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 无条件获取信息
     * @return
     */
    @GetMapping("GetType")
    private String getTypeList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<FraudType> list=fraudTypeimpl.getTypeList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
